package net.mingsoft.report_data.schedule;

import lombok.extern.slf4j.Slf4j;
import net.mingsoft.basic.util.DateUtil;
import net.mingsoft.report_data.dao.ZkDataDao;
import net.mingsoft.report_data.dao.ZkMessageDao;
import net.mingsoft.report_data.entity.ZkDataFocusArea;
import net.mingsoft.report_data.sao.SgSao;
import net.mingsoft.report_data.sao.entity.*;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SgDataSchedule {

    @Resource
    SgSao sgSao;

    @Resource
    ZkMessageDao zkMessageDao;

    private static final List<String> FILTER_FOCUS_AREA = Arrays.asList("骑楼老街", "南山文化旅游区", "海南白石岭旅游景区", "尖峰岭国家森林公园", "呀诺达热带雨林", "美兰机场", "海口秀英港", "海口东站", "凤凰机场", "三亚港", "海南省人民政府", "五源河体育馆", "三亚市人民政府", "琼海市人民政府", "文昌市人民政府");
    private static final String HAINAN_FOCUS_AREA = "海南省关注区域";

    @Scheduled(cron = "0 0 1 * * ?")
    public void saveSgData() {
        // 获取当前所有关注区域
        AllTopDataEntity allTopData = sgSao.getAllTopData();
        List<FocusAreaDto> allFocusAreaData = allTopData.getData();
        List<FocusAreaDto> haiNanAreaList =
                allFocusAreaData.stream().filter(o -> o.getDesc().equals(HAINAN_FOCUS_AREA)).collect(Collectors.toList());
        Map<Integer, List<AreaDetailDto>> haiNanFocusAreaMap = haiNanAreaList.get(0).getMap();
        List<AreaDetailDto> allHainanAreaDtoList = new ArrayList<>();
        haiNanFocusAreaMap.forEach((key, value) -> allHainanAreaDtoList.addAll(value));

        // 根据区域ID和任务ID获取详情
        // List<FocusAreaResponse> areaResponseList = new ArrayList<>();
        List<ZkDataFocusArea> poList = new ArrayList<>();
        allHainanAreaDtoList.stream().filter(o->FILTER_FOCUS_AREA.contains(o.getAreaName())).forEach(area -> {
            // 根据区域ID和任务ID获取 -> 重点区域实时监测-查询实时在线用户数 (/areaMonitoring/getCurrentUser)
            CurrentUserDto currentUserDto = sgSao.getCurrentUser(area.getAreaId(), area.getTaskId());
            poList.add(new ZkDataFocusArea()
                    .setAreaName(area.getAreaName())
                    .setKeyUsers(currentUserDto.getKeyUsers())
                    .setMobileUsers(currentUserDto.getMobileUsers())
                    .setOverseasUsers(currentUserDto.getOverseasUsers())
                    .setPermanentUsers(currentUserDto.getPermanentUsers())
                    .setOtherProvincesUsers(currentUserDto.getOtherProvincesUsers())
                    .setSpecificUsers(currentUserDto.getSpecificUsers()));
        });

        zkMessageDao.insertSgData(poList);

    }
}
